package org.lex.keytool.ui.common;

import java.awt.BorderLayout;
import java.io.IOException;
import java.io.Writer;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class TitledMessagePane extends JPanel {
	private JLabel label;
	private JTextArea textArea;

	private Writer writer = null;

	public TitledMessagePane() {
		this("Message");
	}

	public TitledMessagePane(String title) {
		super(new BorderLayout(0, 0));
		this.initComponents();
		this.setTitle(title);
	}

	public void setTitle(String title) {
		this.label.setText(title);
	}

	public Writer getWriter() {
		if (null == writer) {
			writer = new Writer() {
				@Override
				public void write(char[] cbuf, int off, int len) throws IOException {
					this.write(new String(cbuf, off, len));
				}

				@Override
				public void write(String str, int off, int len) throws IOException {
					textArea.append(str.substring(off, off + len));
				}

				@Override
				public void close() throws IOException {
				}

				@Override
				public void flush() throws IOException {
				}
			};
		}
		return writer;
	}

	private void initComponents() {
		label = new JLabel();
		this.add(label, BorderLayout.PAGE_START);

		textArea = new JTextArea();
		this.add(new JScrollPane(textArea), BorderLayout.CENTER);
	}
}
